package com.mark.mall.admin.platform.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageInfo;
import com.github.pagehelper.page.PageMethod;
import com.mark.mall.admin.platform.dto.PageParamDTO;
import com.mark.mall.admin.platform.dto.ProviderDTO;
import com.mark.mall.admin.platform.dto.ProviderSelectDTO;
import com.mark.mall.admin.platform.dto.param.ProviderAddParam;
import com.mark.mall.admin.platform.dto.param.ProviderUpdateParam;
import com.mark.mall.admin.platform.entity.Product;
import com.mark.mall.admin.platform.entity.Provider;
import com.mark.mall.admin.platform.mapper.ProductMapper;
import com.mark.mall.admin.platform.mapper.ProviderMapper;
import com.mark.mall.admin.platform.service.ProviderService;
import com.mark.mall.common.myException.MyException;
import com.mark.mall.common.resultBean.R;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 供应商表 服务实现类
 * </p>
 *
 * @author admin
 * @since 2021-02-28
 */
@Service
@Slf4j
@RequiredArgsConstructor
public class ProviderServiceImpl extends ServiceImpl<ProviderMapper, Provider> implements ProviderService {
    private final ProductMapper productMapper;
    @Override
    public R<PageInfo<ProviderDTO>> providerPageInfo(PageParamDTO pageParamDTO, String providerName) {
        PageInfo<ProviderDTO> pageInfo = PageMethod.startPage(pageParamDTO.getPageNo(), pageParamDTO.getPageSize())
                .doSelectPageInfo(() -> list(Wrappers.lambdaQuery(BeanUtil.copyProperties(providerName, Provider.class))));
        return new R<>(pageInfo);
    }

    @Override
    public R<Boolean> addProvider(ProviderAddParam param) {
        save(BeanUtil.copyProperties(param, Provider.class));
        return new R<>(true, "新增成功!");
    }

    @Override
    public R<Boolean> delProvider(String ids) {
        if (StringUtils.isEmpty(ids)) {
            throw new MyException("参数缺失,请核实!");
        }
        if (ids.contains(",")) {
            String[] idS = ids.split(",");
            if (idS.length > 0) {
                for (String id : idS) {
                    delProviderCommon(id);
                }
            }
        } else {
            delProviderCommon(ids);
        }
        return new R<>(true, "删除成功!");
    }

    @Override
    public R<Boolean> updateProvider(ProviderUpdateParam param) {
        Provider provider = baseMapper.selectById(param.getId());
        if (provider == null) {
            throw new MyException("id为{" + param.getId() + "}的供应商不存在!");
        }
        BeanUtils.copyProperties(param, provider);
        baseMapper.update(provider, new QueryWrapper<Provider>().lambda().eq(Provider::getId, param.getId()));
        return new R<>(true, "修改成功!");
    }

    @Override
    public R<List<ProviderSelectDTO>> selectList() {
        List<Provider> list = baseMapper.selectList(null);
        List<ProviderSelectDTO> dtoList = new ArrayList<>();
        if (list != null && list.size() > 0) {
            for (Provider provider : list) {
                // 利用构造方法进行参数赋值
                dtoList.add(new ProviderSelectDTO(provider));
            }
        }
        return new R<>(dtoList);
    }

    /**
     * 删除供应商方法封装
     *
     * @param ids
     */
    private void delProviderCommon(String ids) {
        Provider provider = baseMapper.selectById(ids);
        if (provider == null) {
            throw new MyException("id为{" + ids + "}的供应商不存在!");
        }
        // 查询时自动带上状态（逻辑删除）
        Product product = productMapper.selectOne(Wrappers.<Product>lambdaQuery().eq(Product::getProviderId, ids));
        if (product != null) {
            throw new MyException("该供应商，存在关联物料，请先处理物料随后处理供应商！");
        }
        baseMapper.deleteById(ids);
    }
}
